package com.frontend;

import static org.junit.Assert.*;

import java.util.logging.Logger;

import org.junit.Test;
import com.users.User;

/**
 * This Junit tests the methods in the networkinterfacer class
 * 
 * @author Se Hoon Shon
 */
public class TestFrontend {

	public NetworkInterfacer net;

	public void setup() {
		try {
			net = new NetworkInterfacer(Logger.getLogger("TestLogger"));
		} catch (ConnectionException e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * This Junit test tests to see all the method related to login are properly
	 * working. Status of suspension is checked after each number of trial of
	 * logging in.
	 * 
	 * @author Se Hoon Shon
	 */
	@Test
	public void testLogin() {
		try {
			setup();
			User admin = net.login("admin", "admin");
			System.out.println(admin.isSuspended());
			// Check to see if variables of the user matches
			// with the name in the database system.

			assertTrue(admin.getName().equals("Admin"));
			assertTrue(admin.getUser_type() == 0);
			assertTrue(admin.getUsername().equals("admin"));
			// Check if the user is suspended initially
			assertFalse(admin.isSuspended());

			// Try one, two and three error
			// logins and check if suspended
			try{
			net.login("admin", "hungry");
			} catch(LockoutException le){
				assertFalse(true);
			}
			try{
			net.login("admin", "gogo");
			} catch(LockoutException le){
				assertFalse(true);
			}
			try{
			net.login("admin", "fail!");
			} catch(LockoutException le){
				assertFalse(true);
			}
			// Fourth consecutive login. should make user suspended
			try{
				net.login("admin", "hello");
			} catch(LockoutException le){
				assertTrue(true);
			}

			// Un-suspend the user and check again
			//admin.setSuspended(false);\
			net.unlock("admin");

		} catch (LockoutException le) {
			System.out.println("caught");
			
		} catch (ConnectionException ce) {
		}
	}

	@Test
	public void testAuthenticate() {
		try {
			setup();

			User patient = net.login("testpatient", "rawr");
			assertTrue(net.authenticate(patient.getUsername(), "rawr"));

		} catch (LockoutException le) {
		} catch (ConnectionException ce) {
		}

	}
	
	@Test
	public void testAddUser(){
		try{
			setup();
			assertTrue(net.addUser("abcd", "efgh", "hijk", 1));
			net.deleteUser("efgh");
		}
		catch(ConnectionException ce){
		}
	}

}
